package com.egreen.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.egreen.common.pojo.DTO.system.SystemAnnouncementDTO;
import com.egreen.common.pojo.VO.system.GetSystemAnnouncementVO;
import com.egreen.common.pojo.entity.system.SystemAnnouncement;

import com.egreen.system.dao.SystemAnnouncementDao;
import com.egreen.system.ex.SystemException;
import com.egreen.system.service.ISystemAnnouncementService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cloudJiang
 * @since 2022-07-16
 */
@Service
public class SystemAnnouncementServiceImpl extends ServiceImpl<SystemAnnouncementDao, SystemAnnouncement> implements ISystemAnnouncementService {
    @Autowired
    SystemAnnouncementDao announcementDao;

    /**
     * 系统公告查看
     * @return
     */
    @Override
    public List<GetSystemAnnouncementVO> getAllSystemAnnouncement() {
        QueryWrapper wrapper = new QueryWrapper();
        //添加我要查询的字段
        wrapper.select("id", "title", "content", "release_time");
        //查询
        List<GetSystemAnnouncementVO> list = announcementDao.selectList(wrapper);
        return list;
    }

    /**
     * 添加系统公告
     */
    @Override
    public void addSystemAnnouncement(SystemAnnouncementDTO systemAnnouncementDTO) {
        SystemAnnouncement systemAnnouncement = new SystemAnnouncement();
        systemAnnouncement.setId(systemAnnouncementDTO.getId());
        systemAnnouncement.setTitle(systemAnnouncementDTO.getTitle());
        systemAnnouncement.setContent(systemAnnouncementDTO.getContent());
        int insert = announcementDao.insert(systemAnnouncement);

        if (insert != 1) {
            //插入失败，抛出异常
            throw new SystemException("插入失败");
        }
    }

    /**
     * 删除系统公告
     */
    @Override
    public void deleteSystemAnnouncement(int id) {
        int delete = announcementDao.deleteById(id);
        if (delete != 1) {
            //删除失败，抛出异常
            throw new SystemException("删除失败");
        }
    }

}
